package com.nowcoder.chapter3.part41;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

/**
 * 创建线程的示例
 */
public class ThreadDemo1 {

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        // 1
//        new First().start();
//        new First().start();

        // 2
//        new Thread(new Second()).start();
//        new Thread(new Second()).start();

        // 3
        FutureTask<String> task0 = new FutureTask<>(new Third());
        FutureTask<String> task1 = new FutureTask<>(new Third());
        new Thread(task0).start();
        new Thread(task1).start();

        // 主线程
        for (int i = 0; i < 10; i++) {
            System.out.println(Thread.currentThread().getName() + "\t\t" + i);
        }

        // 阻塞方法
        System.out.println("返回值-0:\t" + task0.get());
        System.out.println("返回值-1:\t" + task1.get());
    }

    private static class First extends Thread {
        @Override
        public void run() {
            for (int i = 0; i < 10; i++) {
                System.out.println(Thread.currentThread().getName() + "\t" + i);
            }
        }
    }

    private static class Second implements Runnable {
        @Override
        public void run() {
            for (int i = 0; i < 10; i++) {
                System.out.println(Thread.currentThread().getName() + "\t" + i);
            }
        }
    }

    private static class Third implements Callable<String> {
        @Override
        public String call() throws Exception {
            for (int i = 0; i < 10; i++) {
                System.out.println(Thread.currentThread().getName() + "\t" + i);
            }
            return Thread.currentThread().getName();
        }
    }

}
